//
//  MBWLMListCell.m
//  Meboo
//
//  Created by kyo on 2023/10/25.
//

#import "MBWLMListCell.h"
#import "MBWLMTopicImageModel.h"
#import "NSMatchViewController.h"
#import "FXBlurView.h"
#import "MBThumbnailCache.h"
@interface MBWLMListCell()
@property(nonatomic,strong) UIImageView *iconImageView;
@property(nonatomic,strong) UILabel *nameLab;

@property(nonatomic,strong) UILabel *interestLab;
@property(nonatomic,strong) UIButton *likeBtn;
@property(nonatomic,strong) UIButton *passBtn;

@property (nonatomic, strong) UIVisualEffectView *effectView;
@property (nonatomic, strong) UIImageView *starImageView;

@property (nonatomic, strong) UIView *shadowView;

@property (nonatomic, strong) FXBlurView *blurView;

//轮播图 顶部遮罩 --- 目的:防止图片未加载出来时,白底无法清楚显示pageview和online/nearby
@property (nonatomic, strong) UIView *topShadowView;
@property(nonatomic,strong) UIButton *tagBtn1;
@end

@implementation MBWLMListCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Public Metohd

- (void)setModel:(MBWLMTopicModel *)model {
    _model = model;
    if ([MBUserAccount defaultAccount].isVip) {
        //会员直接sd加载
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.avatarUrl]];
    } else {
//        //非会员 先检查缓存有没有
//        UIImage *image = [[MBThumbnailCache sharedCache] getThumbnailImageForKey:[NSString stringWithFormat:@"wlm%@",model.avatarUrl]];
//        if (image == nil) {
//            //没有缓存 重新加载并存储
//            [self getImage:model.avatarUrl];
//        } else {
//            //有缓存 直接展示
//            self.iconImageView.image = image;
//        }
        
        self.iconImageView.hidden = YES;
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.avatarUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            self.iconImageView.image = [image blurredImageWithRadius:BlurValue iterations:2 tintColor:HEXS(@"000000")];
            self.iconImageView.hidden = NO;
        }];
    }
    
    if ([model.online isEqualToString:@"1"]) {
        self.tagBtn1.hidden = NO;
    } else {
        self.tagBtn1.hidden = YES;
    }
//    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.avatarUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        // 当图片下载完成后，这里的代码会被调用
////        [self.iconImageView setImageToBlur: image blurRadius:40 completionBlock:nil];
//    }];
//    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.avatarUrl]];
    
    self.nameLab.text = [NSString stringWithFormat:@"%@,%@",model.nickName,model.age];
    
    if (self.model.interests.count > 1) {
        self.interestLab.text = [NSString stringWithFormat:@"%@, %@",model.interests[0].interest,model.interests[1].interest];
    } else if (self.model.interests.count == 1) {
        self.interestLab.text = [NSString stringWithFormat:@"%@",model.interests[0].interest];
    } else {
        self.interestLab.hidden = YES;
    }
    
    
    if ([MBUserAccount defaultAccount].isVip) {
        self.starImageView.hidden = YES;
        self.passBtn.hidden = NO;
        self.likeBtn.hidden = NO;
        self.topShadowView.hidden = NO;
        [self.interestLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(MBHeightScale(-46));
        }];
    } else {
        self.starImageView.hidden = NO;
        self.passBtn.hidden = YES;
        self.likeBtn.hidden = YES;
        self.topShadowView.hidden = YES;
        [self.interestLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(MBHeightScale(-16));
        }];
    }
}

- (void)getImage:(NSString *)urlStr {
    [[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:urlStr] options:SDWebImageRetryFailed progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
//        [self.iconImageView setImageToBlur: image blurRadius:14 completionBlock:nil];
        if (finished) {
            self.iconImageView.image = [image blurredImageWithRadius:BlurValue iterations:2 tintColor:HEXS(@"000000")];
            [[MBThumbnailCache sharedCache]  setThumbnailImage:self.iconImageView.image forKey:[NSString stringWithFormat:@"wlm%@",urlStr]];
        }
    }];
}

 
#pragma mark - Action
- (void)passAction {
    if (self.passBlock) {
        self.passBlock();
    }
    //debugTest
    //测试的时候临时注释 因为划了就没了
    self.userInteractionEnabled = NO;
    [[MBWhoLikeMeDataManager sharedManager] noticSayHiWithType:2 userId:self.model.userCodeFriend ?: self.model.friendUserCode success:^(BOOL success) {
        self.userInteractionEnabled = YES;
    }];
    
    /**
     埋点
     点击不喜欢WLM_Pass
     */
    [[NSIOApiManager sharedManager] setIOWithEventName:@"WLM_Pass"];
}


- (void)likeAction {
    __WeakSelf
    //debugTest  wlm数据有限,划了就没了 临时注释
    [MBProgressHUD showActivityMessageInView:@"matching..."];
    self.userInteractionEnabled = NO;
    [[MBWhoLikeMeDataManager sharedManager] noticSayHiWithType:1 userId:self.model.userCodeFriend ?: self.model.friendUserCode success:^(BOOL success) {
        weakSelf.userInteractionEnabled = YES;
        [MBProgressHUD hideHUD];
        if (success) {
            NSMatchViewController *matchViewController = [NSMatchViewController new];
            matchViewController.model = self.model;
            [weakSelf.currentViewController.navigationController pushViewController:matchViewController animated:YES];
            if (weakSelf.passBlock) {
                weakSelf.passBlock();
            }
        }
    }];
    
    /**
     埋点
     点击喜欢WLM_Like
     */
    [[NSIOApiManager sharedManager] setIOWithEventName:@"WLM_Like"];
}

#pragma mark - UI
- (void)setupUI {
    self.contentView.backgroundColor = HEXS(@"ffffff");
    self.contentView.layer.cornerRadius = MBHeightScale(12);
    self.contentView.layer.masksToBounds = YES;
    
    [self.contentView addSubview:self.iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self.contentView addSubview:self.topShadowView];
    [self.topShadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.contentView);
        make.height.mas_equalTo(MBHeightScale(29));
    }];
    
    [self.contentView addSubview:self.tagBtn1];
    [self.tagBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBHeightScale(10));
        make.centerY.equalTo(self.topShadowView);
    }];
    
    [self.contentView addSubview:self.shadowView];
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(MBHeightScale(99));
    }];
    
    [self.iconImageView layoutIfNeeded];
    
    if (![MBUserAccount defaultAccount].isVip) {
//        [self.iconImageView addSubview:self.effectView];
//        [self.effectView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(self.iconImageView);
//        }];
        
//        [self.iconImageView addSubview:self.blurView];
//        [self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(self.iconImageView);
//        }];
//        
        [self addSubview:self.starImageView];
        [self.starImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.iconImageView);
            make.width.height.mas_equalTo(MBHeightScale(40));
        }];
    }
    
    [self.contentView addSubview:self.passBtn];
    [self.passBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(MBHeightScale(-10));
        make.left.mas_equalTo(MBWidthScale(10));
        make.right.equalTo(self.contentView.mas_centerX).offset(MBWidthScale(-3));
        make.height.mas_equalTo(MBHeightScale(26));
    }];
    
    [self.contentView addSubview:self.likeBtn];
    [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(MBHeightScale(-10));
        make.right.mas_equalTo(MBWidthScale(-10));
        make.left.equalTo(self.contentView.mas_centerX).offset(MBWidthScale(3));
        make.height.mas_equalTo(MBHeightScale(26));
    }];

    
    [self.contentView addSubview:self.interestLab];
    [self.interestLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(MBHeightScale(-16));
        make.left.mas_equalTo(MBWidthScale(10));
        make.right.mas_equalTo(MBWidthScale(-10));
    }];
    
    [self.contentView addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.interestLab.mas_top).offset(MBHeightScale(-5));
        make.left.mas_equalTo(MBWidthScale(10));
        make.right.mas_equalTo(MBWidthScale(-10));
    }];
    
    if ([MBUserAccount defaultAccount].isVip) {
        self.passBtn.hidden = NO;
        self.likeBtn.hidden = NO;
        self.topShadowView.hidden = NO;
        [self.interestLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(MBHeightScale(-46));
        }];
    }
}

#pragma mark - Setter && Getter
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
//        _iconImageView.backgroundColor = UIColor.whiteColor;
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        _iconImageView.clipsToBounds = YES;
    }
    return _iconImageView;
}
- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentCenter;
        _nameLab.font = MBHelBoldFontSizeScale(15);
        _nameLab.textColor = HEXS(@"ffffff");
    }
    return _nameLab;
}

- (UILabel *)interestLab {
    if (!_interestLab) {
        _interestLab = [UILabel new];
        _interestLab.textAlignment = NSTextAlignmentCenter;
        _interestLab.font = MBHelFontSizeScale(13);
        _interestLab.textColor = HEXS(@"ffffff");
    }
    return _interestLab;
}

- (UIButton *)likeBtn {
    if (!_likeBtn) {
        _likeBtn = [UIButton new];
        _likeBtn.hidden = YES;
        _likeBtn.backgroundColor = HEXS(@"ffffff");
        [_likeBtn setImage:ImageWithName(@"wholikeme_like") forState:0];
        _likeBtn.layer.cornerRadius = MBHeightScale(13);
        [_likeBtn addTarget:self action:@selector(likeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _likeBtn;
}

- (UIButton *)passBtn {
    if (!_passBtn) {
        _passBtn = [UIButton new];
        _passBtn.hidden = YES;
        _passBtn.backgroundColor = HEXS(@"ffffff");
        [_passBtn setImage:ImageWithName(@"wholikeme_pass") forState:0];
        _passBtn.layer.cornerRadius = MBHeightScale(13);
        [_passBtn addTarget:self action:@selector(passAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _passBtn;
}

- (UIVisualEffectView *)effectView {
    if (!_effectView) {
        UIBlurEffect *blurEffrct = [UIBlurEffect effectWithStyle:UIBlurEffectStyleSystemUltraThinMaterialDark];
        _effectView = [[UIVisualEffectView alloc]initWithEffect:blurEffrct];
//        _effectView.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.5];
        _effectView.alpha = 0.90;
//        _effectView.hidden = YES;
    }
    return _effectView;
}

- (UIImageView *)starImageView {
    if (!_starImageView) {
        _starImageView = [UIImageView new];
        _starImageView.image = ImageWithName(@"wlm_effect_star");
//        _starImageView.hidden = YES;
    }
    return _starImageView;
}

- (UIView *)shadowView {
    if (!_shadowView) {
        _shadowView = [UIView new];
        //渐变色 设置开始和结束位置(设置渐变的方向)
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.startPoint = CGPointMake(0, 0);
        gradient.endPoint = CGPointMake(0, 1);
        gradient.locations = @[@(0.0),@(1.0f)];
        gradient.frame =CGRectMake(0,0,(ScreenWidth-MBWidthScale(46))/2,MBHeightScale(99));
        gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0].CGColor,(id)[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.5].CGColor,nil];
        [_shadowView.layer insertSublayer:gradient atIndex:0];
    }
    return _shadowView;
}

- (UIView *)topShadowView {
    if (!_topShadowView) {
        _topShadowView = [UIView new];
        _topShadowView.hidden = YES;
        //渐变色 设置开始和结束位置(设置渐变的方向)
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.startPoint = CGPointMake(0, 0);
        gradient.endPoint = CGPointMake(0, 1);
        gradient.locations = @[@(0.0),@(1.0f)];
        gradient.frame =CGRectMake(0,0,(ScreenWidth-MBWidthScale(46))/2,MBHeightScale(29));
        gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.5].CGColor,(id)[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0].CGColor,nil];
        [_topShadowView.layer insertSublayer:gradient atIndex:0];
    }
    return _topShadowView;
}

- (UIButton *)tagBtn1 {
    if (!_tagBtn1) {
        _tagBtn1 = [UIButton leftimageAndRighttitlePatternWithImage:@"wlm_list_online" AndMargin:4 AndTitle:@"Online" AndTitleFont:MBHelFontSizeScale(12) AndTitleColor:HEXS(@"ffffff")];
    }
    return _tagBtn1;
}
@end
